package com.socialbank.web.service.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.socialbank.web.FinanceBean.BankFund;
import com.socialbank.web.bean.ResultMessage;
import com.socialbank.web.mapper.BankCertificateDepositsMapper;
import com.socialbank.web.mapper.BankFundMapper;
import com.socialbank.web.service.FundService;
import com.wqdata.auth.util.ExcelUtils;
import com.wqdata.auth.util.UserUtils;
@Service
public class BankFundServiceImpl implements FundService{
	@Autowired
	private SqlSession sqlSession;

	private static final Integer TYPE=5;
	private int code;
	private String msg;
	@Override
	public void save(MultipartFile multiparFile) {
		// TODO Auto-generated method stub
		//从excel表导入数据
		BankFundMapper mapper = sqlSession.getMapper(BankFundMapper.class);
		Long deptId=UserUtils.getCurrrentDeptId();//获取部门id
		if(!multiparFile.isEmpty()){
			CommonsMultipartFile cf= (CommonsMultipartFile)multiparFile;
			DiskFileItem fi = (DiskFileItem)cf.getFileItem(); 
			File newFile = fi.getStoreLocation();
			List<String> list = new ArrayList<>();
			list.add("fundType");
			list.add("productCode");
			list.add("productName");
			list.add("fundCompany");
			list.add("riskType");
			list.add("fundStatus");
			ExcelUtils.setColumns(list);
			List<BankFund> funds = ExcelUtils.readExcel(newFile,BankFund.class);

			boolean flag;
			String name="";
			List<String> names=mapper.getNameList(deptId);
			BankFund fund;
			for(int i=0;i<funds.size();i++){
				funds.get(i).setDeptId(deptId);
				fund=funds.get(i);
				name=fund.getProductName().trim();
				flag=names.contains(name);
				if(flag){
					mapper.deleteByName(fund);
				}
			}
			mapper.save(funds);

		}

	}


	@Override
	public BankFund findById(String id) {
		// TODO Auto-generated method stub
		BankFundMapper mapper = sqlSession.getMapper(BankFundMapper.class);
		BankFund fund=new BankFund();
		fund=mapper.findById(id);
		return fund;
	}


	@Override
	public List<BankFund> getList(String fundType) {
		// TODO Auto-generated method stub
		BankFundMapper mapper = sqlSession.getMapper(BankFundMapper.class);
		if("null".equals(fundType)){
			fundType=null;
		}
		Long deptId=UserUtils.getCurrrentDeptId();//获取部门id

		List<BankFund> manages=mapper.getList(deptId,fundType);
		return manages;
	}


	@Override
	public Object deleteById(String id) {
		// TODO Auto-generated method stub
		BankFundMapper mapper = sqlSession.getMapper(BankFundMapper.class);
		List<String> ids=Arrays.asList(id.split(","));
		mapper.deleteById(ids);
		return new ResultMessage(200,"",null);

	}


	@Override
	public List<BankFund> getListAll(Long deptIp,String fundType) {
		// TODO Auto-generated method stub
		BankFundMapper mapper = sqlSession.getMapper(BankFundMapper.class);
		List<BankFund> list=new ArrayList<BankFund>();
		list=mapper.getList(deptIp,fundType);
		return list;
	}


}
